import { Behaviour } from "../Behaviour";
import { TextRenderer } from "../TextRenderer";
import { getGameObjectById } from "../../engine";
import { BitmapRenderer } from "../BitmapRenderer";
import { number } from "../validators/number";
import { System } from "./System";
import { Clock } from "../../behaviours/Clock";


let interval = -1
let minute = -1 //设置文字的时候minute后面加一个0
let hour = 16
let sun = 0; //换太阳图片用

export class ClockSystem extends System {

    //开始计时的flag
    start = false
    dayEnd = false

    onStart(){
        getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text = "16：00"
        //先调中转方法
        this.zhongzhuan()

    } 


    
    //中转判断计时开始
    zhongzhuan(){
        setTimeout(() => {
            //从中转站clock拿start:是否开始走表(每次开始走表都会重置时间)
            this.start = getGameObjectById('UI_time_text').getBehaviour(Clock).start
            if(this.start){
                    hour = 16
                    minute = -1
                    sun = 0
                this.zxwTime()
                console.log("开始计时")
                //start再设回false
                //zhongzhuan方法循环调用等待下一天再次设start = true,开始走表
                getGameObjectById('UI_time_text').getBehaviour(Clock).start = false
            }           
            this.zhongzhuan()
        }, 1000);
    }

    //走表最后停在24：00的方法
    zxwTime(){ 
            if(hour < 24){
                minute ++
                console.log("minute: " + minute + ";  hour: " + hour)
                if (minute > 5) {//一个小时走完
                    //变换按钮文字
                    minute = 0 //归零
                    hour++
                    sun ++
                    getGameObjectById('UI_time_sun2').getBehaviour(BitmapRenderer).source =  "./assets/images/UI_time_" + sun + "sun.png"
                } 
            }else{
                getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text = "24：00"
                getGameObjectById('UI_time_sun2').getBehaviour(BitmapRenderer).source =  "./assets/images/UI_time_8sun.png"
                console.log("游戏结束")
                return 999
            }
            getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text = hour.toString() + "：" + minute.toString()+"0"
        if(getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text === "20：00"){
            getGameObjectById('schoolgatebackground_day').getBehaviour(BitmapRenderer).source =  "./assets/images/background_school_night.png"
        } 
        
        setTimeout(() => {
            this.dayEnd = getGameObjectById('UI_time_text').getBehaviour(Clock).dayEnd
            //console.log("今天的状态dayEnd：" + this.dayEnd)
            //console.log("太阳图标：" + getGameObjectById('UI_time_sun2').getBehaviour(BitmapRenderer).source)
            if(!this.dayEnd){//如果一天还没结束就继续调用
                this.zxwTime()
            }else{
                console.log("```````````愉快的一天结束啦\^o^/`````````````````````````````")
                getGameObjectById('UI_time_text').getBehaviour(Clock).dayEnd = false
                // getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text = "24：00"
                // getGameObjectById('UI_time_sun2').getBehaviour(BitmapRenderer).source =  "./assets/images/UI_time_0sun.png"
                this.dayEnd = false
                //console.log("dayEnd应该是false！！：" + )
                return 444
            }
            
        }, 5000);//5000毫秒更新10min


    } 
    
    // interval = setInterval(() => {
    //     if(hour < 24){
    //         minute ++
    //         console.log(minute)
    //         console.log(hour)
    //         if (minute > 5) {//一个小时走完
    //             //变换按钮文字
    //             minute = 0 //归零
    //             hour++
    //         } 
    //     }else{
    //         getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text = "24：00"
    //     }
    //     getGameObjectById('UI_time_text').getBehaviour(TextRenderer).text = hour.toString() + "：" + minute.toString()+"0"
    // }, 1000);//5000毫秒更新10min
}